$(function(){
	var po_datagrid = $("#po_datagrid");
	var po1_datagrid = $("#po1_datagrid");
	var poadd_datagrid = $("#po1_datagrid");
	var po_dialog = $("#po_dialog");
	var po1_dialog = $("#po1_dialog");
	var poadd_datagrid = $("#po1_dialog");


	//订单列表
    po_datagrid.datagrid({
		url:"purchaseOrder/list",
		fit:true,
        singleSelect:true,
		columns:[[
		          {field:'x',checkbox:true},
		          {field:'id',title:'',width:100,align:'center' ,hidden:true},
		          {field:'orderSn',title:'订单编码',width:100,align:'center'},
		          {field:'depot',title:'存储仓库',width:100,align:'center',formatter:function (value, row, index) {
					  if(value){
					  	return value.name;
					  }
                  }},
		          {field:'totalNumber',title:'订单总数量',width:100,align:'center'},
		          {field:'totalAmount',title:'订单总金额',width:100,align:'center'},
		          {field:'payAmount',title:'实际付款金额',width:100,align:'center'},
		          {field:'inputTime',title:'添加时间',width:100,align:'center'},
		          {field:'inputUser',title:'订单处理人',width:100,align:'center',formatter:function (value,row,index) {
					  if (value){
					  	return value.username;
					  }
                  }},
		          {field:'auditTime',title:'审核时间',width:100,align:'center'},
		          {field:'auditor',title:'订单审核人',width:100,align:'center',formatter:function (value,row,index) {
					  if (value){
					  	return value.username;
					  }
                  }},
		          {field:'state',title:'入库状态',width:100,align:'center',formatter:function (value,row,index) {
					  if(value==1){
					  	return "已审核";
					  }else if(value ==0){
					  	return "待审核"
					  }
                  }},

		          ]],
		fitColumns:true,
		toolbar:"#tb",
		pagination:true,
        onClickRow:function (index,row) {
            po1_datagrid.datagrid({
                url:"purchaseOrderItem/queryItemByOrderId?orderId="+(index+1),
                fit:true,
                singleSelect:true,
                columns:[[
                    {field:'x',checkbox:true},
                    {field:'id',title:'编号',width:100,align:'center' ,hidden:true},
                    {field:'product',title:'商品名称',width:100,align:'center',formatter:function (value, row, index) {
						if (value){
							return value.name;
						}
                    }},
                    /*{field:'product',title:'商品编码',width:100,align:'center',formatter:function (value, row, index) {
						if (value){
							return value.sn;
						}
                    }},
                    {field:'product',title:'商品规格',width:100,align:'center',formatter:function (value, row, index) {
						if(row){
							return row.size;
						}
                    }},*/
                    {field:'costPrice',title:'单价',width:100,align:'center'},
                    {field:'number',title:'商品数量',width:100,align:'center'},
                    {field:'amount',title:'商品总价值',width:100,align:'center'},
                    {field:'remark',title:'备注',width:100,align:'center'},
                ]],
                fitColumns:true,
                toolbar:"#btt",
                pagination:true

            });
		}

	});




	//创建一个用于添加订单的对话框
	po_dialog.dialog({
		width:600,
		height:300,
		buttons:"#bb",
		closed:"true"
        //订单编辑页面

	})

    poadd_datagrid.datagrid({
        url:"",
        fit:true,
        singleSelect:true,
        columns:[[
            {field:'id',title:'',width:100,align:'center' ,hidden:true},
            {field:'orderSn',title:'订单编码',width:100,align:'center'},
            {field:'depot',title:'存储仓库',width:100,align:'center'},
            {field:'totalNumber',title:'订单总数量',width:100,align:'center'},
            {field:'totalAmount',title:'订单总金额',width:100,align:'center'},
            {field:'payAmount',title:'实际付款金额',width:100,align:'center'},
            {field:'inputTime',title:'添加时间',width:100,align:'center'},
            {field:'inputUser',title:'订单处理人',width:100,align:'center'},
            {field:'auditTime',title:'审核时间',width:100,align:'center'},
            {field:'auditor',title:'订单审核人',width:100,align:'center'},
            {field:'state',title:'入库状态',width:100,align:'center'},

        ]],
        fitColumns:true,
        toolbar:"#tb"

    })



    var cmdObj={
        //工具栏的刷新
        reload: function () {
            //刷新操作
           	 po_datagrid.datagrid("reload");
        },
        //添加操作
        add: function(){
			//因为编辑与添加共用一个对话框,所以在操作之前应将之前的数据全部清空
			$("#editForm").form("clear");
			//打开添加对话框
			po_dialog.dialog("open");
            po_dialog.dialog("setTitle","添加订单");
    	},


        //保存操作
        save: function(){
            //获取id
            var id = $("#poId").val();
            var url = "purchaseOrder/save";

            //提交表单
            $("#editForm").form("submit",{
                //表单提交的路径
                url:url,
				onSubmit:function (param) {//表单提交前修改参数

				 },
                //操作成功之后执行函数,data是返回的数据
                //注意:data是json格式的字符串
                success:function(data){
                    //1.接收返回的数据
                    //2.判断操作是否成功
                    data=$.parseJSON(data);
                    //3.操作失败,提示用户
                    if(!data.success){
                        $.messager.alert('温馨提示',data.msg);
                    }else{
                        //操作成功
                        //1.提示用户操作成功
                        //2.关闭当前的输入对话框
                        po_dialog.dialog("close");
                        //3.刷新datagrid的数据
                        po_datagrid.datagrid("reload");
                    }
                }
            })
        },
        audit:function(){
            var row = po_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert('温馨提示','请选择要审核的行');
            }else{
                var id = row.id;
                $.get("purchaseOrder/audit",{id:id},function (data) {
                    if(data.success){
                        $.messager.alert('温馨提示',"审核成功");
                        po_datagrid.datagrid("reload");
                    }else{
                        $.messager.alert('温馨提示',data.msg);
                    }
                })

            }
		},

        //关闭对话框
        cancel: function(){
			po_dialog.dialog("close");
            //record_dialog.dialog("close");
		},
        //删除操作
        remove: function(){
            var row = po_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示','请选择要编辑的行');
            }else{
                //使用ajax发起修改状态请求
                $.get("purchaseOrder/delete",{id:row.id},function(data){
                    if (!data.success) {
                        $.messager.alert('温馨提示',data.msg);
                    }else{
                        //刷新列表数据
                        po_datagrid.datagrid("reload");
                    }
                })
            }
        },

		//高级查询
        query: function(){
			//因为easyui对元素的html进行相关的封装,可能导致获取到的数据不准确,建议都是使用easyui提供的获取数据的方法
			//var keywords = $(#"keywords").val();
			var keywords = $("#keywords").textbox("getValue");

			//所谓高级查询,无非是让datagrid根据条件去更新列表数据,所以只需要刷新列表,同时携带查询条件即可
			po_datagrid.datagrid("load",{
				keywords:keywords
			});
		}
	}

    //调用
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });

});
